﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OUSMS.AppLogic.BusinessLogicLayer;
using OUSMS.AppLogic.DataAccessLayer;
using OUSMS.Helpers;
using System.Web.ModelBinding;
namespace OUSMS
{
    public partial class SecondDepartmentList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DepartmentsBLL departmentBLL = new DepartmentsBLL();
                Departments departments = departmentBLL.GetDepartmentByID(Convert.ToInt32(Request.QueryString["DepartmentID"]));
                departmentNameLbl.Text = departments.Name;
            }
        }

        // 返回类型可以更改为 IEnumerable，但是为了支持
        // 分页和排序，必须添加以下参数:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<SecondDepartments> DepartmentssGridView_GetData(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            SecondDepartmentsBLL departmentsBLL = new SecondDepartmentsBLL();
            return departmentsBLL.GetAllActiveSecondDepartmentsByDepartmentID(maximumRows, startRowIndex, out totalRowCount, sortByExpression, Convert.ToInt32(Request.QueryString["DepartmentID"]));
        }

        protected void DepartmentsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label indexLbl = e.Row.FindControl("indexLbl") as Label;
                indexLbl.Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void addDepartmentBtn_Click(object sender, EventArgs e)
        {
            try
            {
                SecondDepartments departments = new SecondDepartments();
                departments.Name = departmentNameTxtBox.Text.Trim();
                departments.IsDeleted = false;
                departments.DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);
                SecondDepartmentsBLL departmentsBLL = new SecondDepartmentsBLL();
                departmentsBLL.AddSecondDepartment(departments);

                DepartmentsGridView.DataBind();
                msgLbl.Text = departments.Name + "已添加成功。";
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                msgLbl.Text = string.Format("添加错误:{0}", ex.Message);
            }
        }

        protected void deleteLnkBtn_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton deleteLnkBtn = sender as LinkButton;
                SecondDepartmentsBLL departmentsBLL = new SecondDepartmentsBLL();
                SecondDepartments department2Delete = departmentsBLL.GetSecondDepartmentByID(Convert.ToInt32(deleteLnkBtn.CommandArgument));
                department2Delete.IsDeleted = true;
                departmentsBLL.SaveChanges();

                DepartmentsGridView.DataBind();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                msgLbl.Text = string.Format("记录删除失败，失败原因:{0}", ex.Message);
            }
        }
    }
}